const Koa = require('koa');
const axios = require('axios')

const app = new Koa();

// 因为是洋葱模型，所以最后的中间件执行成功后赋值，第一个中间件想拿到该值，必须写在next之后
app.use(async (ctx, next) => {
  await next();
  const val = ctx.val;
  console.log(val)
});

// 终极异步解决方案： async
app.use(async (ctx, next) => {
  let start = new Date().getTime();
  const abc = await axios.get('http://www.baidu.com');
  let end = new Date().getTime();
  ctx.val = abc;
  console.log(end - start);
  await next();
});

app.listen(3000);